import pandas as pd
import jieba
from collections import Counter
from config import wconfig


def read_file(filename=wconfig.VOCAB_EN_TEXT_PATH):
    with open(filename, 'r') as f:
        file_context = f.readlines()

    return ' '.join(file_context).split(' ')


def read_zh_file(filename=wconfig.VOCAB_ZH_TEXT_PATH):
    with open(filename, 'r') as f:
        file_context = f.readlines()

    file_context = ' '.join(file_context)
    file_context = jieba.cut(file_context)
    return [val for val in file_context]


def build_vocab(word_list, out_filename=wconfig.VOCAB_EN_PATH):
    if word_list is None:
        return []

    counter = Counter(word_list).most_common()
    data = {idx: [word_count[0], word_count[1]] for idx, word_count in enumerate(counter)}
    print(data)

    pd_data = pd.DataFrame.from_dict(data, orient='index')
    pd_data.to_csv(out_filename, index_label="order", header=['word', 'count'])


if __name__ == '__main__':
    context = read_file()
    build_vocab(context)
    context = read_zh_file()
    build_vocab(context, out_filename=wconfig.VOCAB_ZH_PATH)
